package com.zjh.springcloud.controller;

import com.zjh.common.R;
import com.zjh.entity.Menu;
import com.zjh.entity.Type;
import com.zjh.springcloud.feign.MenuFeign;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * ClassName: MenuClientController
 * Description:
 * @Author zjh
 * @Create 2023/9/28 11:40
 */
@Controller
@RequestMapping("/menuClient")
public class MenuClientController {
	
	@Resource
	private MenuFeign menuFeign;
	
	@GetMapping("/menu_manage")
	public String ToMenuManage(){
		return "menu_manage";
	}
	@GetMapping("/findAll")
	@ResponseBody
	public R<List<Menu>> pageList(@RequestParam("page") Integer page, @RequestParam("limit") Integer limit) {
		return menuFeign.pageList((page-1)*limit, limit);
	}
	
	@GetMapping("/deleteById/{id}")
	public String deleteById(@PathVariable("id") Integer id){
		menuFeign.deleteById(id);
		return "redirect:/menuClient/menu_manage";
	}
	
	@GetMapping("/findById/{id}")
	public String findByIdOne(@PathVariable("id") Integer id, ModelMap map){
		Menu byIdOne = menuFeign.findByIdOne(id);
		List<Type> allTypes = menuFeign.findAllTypes();
		map.addAttribute("menu",byIdOne);
		map.addAttribute("types",allTypes);
		return "menu_update";
	}
	//修改
	@PostMapping("/update")
	public String updateOne(Menu menu){
		menuFeign.updateOne(menu);
		return "redirect:/menuClient/menu_manage";
	}
	
	@GetMapping("toSaveMenu")
	public String toSaveMenu(ModelMap map){
		List<Type> types=menuFeign.queryNameById();
		map.addAttribute("types",types);
		return "menu_add";
	}
	
	@PostMapping("save")
	public String save(Menu menu){
		
		menuFeign.saveMenu(menu);
		return "redirect:/menuClient/menu_manage";
	}
}
